/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.shef.zeno.mywoz;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import uk.ac.shef.zeno.utils.Utils;
/**
 *
 * @author samf
 */
public class WozApp {

    WozState state = WozState.NOTHINGNESS;
    ButtonWizard wizard;
    Robot robot;
//    private HashSet<String> foods;
    LinkedList<String> queue;
    boolean speaking = false;
    Map<String, String> animSpeech = null;
    Map<String, String> questionResponses = null;
    int day;
    void start() {
        wizard = new ButtonWizard(this);
        queue = new LinkedList<>();
        robot = new Robot();
        HashMap<String, String> configs = Utils.readConfig();
        day = Integer.parseInt(configs.get("day"));
        animSpeech = new HashMap<>();
        animSpeech.put("Left_Hand_Up", "Left hand up.");
        animSpeech.put("Right_Hand_Up", "Right hand up.");
        animSpeech.put("Both_Hands_Up", "Both hands up.");
        questionResponses = Utils.readMap("resources/question-responses.txt");
  //      foods = Utils.readSet("resources/foods.txt");
     
        wizard.start();
        
    }

    public void addRequest(String text) {
        queue.add(text);
    }

    public void update() {
        robot.flushQueue();
        wizard.update();
        if (state == WozState.NOTHINGNESS) {
            wizard.disableAllExcept(new String[] {"User_Ready"});
            if (queue.isEmpty()) {
                return;
            }
            String text = queue.poll();
            if (text.equals("User_Ready")) {
                state = WozState.USER_READY;
            } else {
                System.err.println("Unexpected input");
                System.exit(1);
            }
        }

        if (state == WozState.USER_READY) {
            wizard.disableAll();
            robot.addSpeech("OK now it's my turn to play Simon Says. But this time you will tell me what to do! You can tell me to put my left hand up, put my right hand up or put both hands up!");
            state = WozState.SIMON_SAYS;
        }

        if (state == WozState.SIMON_SAYS) {
            wizard.disableAllExcept(new String[] {"Left_Hand_Up", "Right_Hand_Up", "Both_Hands_Up", "Hesitant", "Unrecognized", "Full_Response"});
            if (queue.isEmpty()) {
                return;
            }
            
            String text = queue.poll();
            //if (gestures.contains(text)) {
            if (robot.hasAnimation(text)) {
                //robot.addSpeech("Doing " + text);
                String animSpeechText = animSpeech.get(text);
                robot.addSpeech(animSpeechText);
                robot.addAnimation(text);
            } else if (text.equals("Hesitant")) {
                robot.addSpeech("Please don't be shy. You can tell me to put my left hand up, put my right hand up or put both hands up!");
            } else if (text.equals("Unrecognized")) {
                robot.addSpeech("I can't do that one. You can tell me to put my left hand up, put my right hand up or put both hands up!");
            } else if (text.equals("Full_Response")) {
                wizard.disableAll();
                robot.addSpeech("OK that's enough Simon Says. You are much better than me at this game! Let's do something else now.");
                state = WozState.FOOD_TALK;
            }
        }

        if (state == WozState.FOOD_TALK) {
            if (day==1) {
                robot.addSpeech("I want to talk about healthy living. One important part of healthy living is eating good, healthy food."
                    + " Can you tell me about your favourite foods?");
            } else if (day==2) {
                robot.addSpeech("The other day we talked about eating healthy foods. Another part of healthy living is exercise. "
                        + "This could be sports like football or netball, or it could be every day activities like walking. Can you tell me about"
                        + " your favourite exercise that you like to do?");
            
            } else {
                System.out.println("Wrong day!");
                System.exit(1);
            }
            
            state = WozState.FOOD_PROMPT;
        }

        if (state == WozState.FOOD_PROMPT) {
            wizard.disableAllExcept(new String[] {"Prompt", "Hesitant", "Full_Response"});
            if (queue.isEmpty()) {
                return;
            }
            String text = queue.poll();

            if (text.equals("Prompt")) {
                String promptPhrase = null;
                if (day==1) {
                    promptPhrase = PromptGenerator.nextFoodPrompt();
                } else if (day==2) {
                    promptPhrase = PromptGenerator.nextExercisePrompt();
                } else {
                    System.out.println("Wrong day!");
                    System.exit(1);
                }
                robot.addSpeech(promptPhrase);
            }
            if (text.equals("Hesitant")) {
                if (day==1) {
                    robot.addSpeech("Please don't be shy. Tell me about your favourite food!");
                } else if (day==2) {
                    robot.addSpeech("Please don't be shy. Tell me about your favourite exercises!");
                } else {
                    System.out.println("Wrong day!");
                    System.exit(1);
                }
            } else if (text.equals("Full_Response")) {
                wizard.disableAll();
                if (day==1) {
                   robot.addSpeech("OK great, thank you for telling me about your favourite foods. Now, do you have any questions for me?");
                } else if (day==2) {
                    robot.addSpeech("OK great, thank you for telling me about your favourite exercises. Now, do you have any questions for me?");
                } else {
                    System.out.println("Wrong day!");
                    System.exit(1);
                }               
                state = WozState.QUESTIONS;
            }
        }
        if (state == WozState.QUESTIONS) {
            wizard.disableAllExcept(new String[] {
                "Prompt", "Hesitant", "Full_Response",
                "Doctor?", "School?", "Drive?",
                "Family?", "Clean?", "Where-Live?",
                "Birthday?", "Sleep?", "How-made?",
                "Where-From?", "Struck-Lightning?"
            });

            if (queue.isEmpty()) {
                return;
            }
            String text = queue.poll();

            if (text.equals("Prompt")) {
                String promptPhrase = PromptGenerator.nextQuestionPrompt();
                robot.addSpeech(promptPhrase);
            }
            else if (text.equals("Hesitant")) {
                robot.addSpeech("Please don't be shy. You can ask me anything!");
            } else if (text.equals("Full_Response")) {
                robot.addSpeech("OK thank you. It was great to talk to you. Goodbye for now, but I hope to see you again soon! Goodbye!");
                state = WozState.FINISH;
            } else {
                if (questionResponses.containsKey(text)) {
                    String response = questionResponses.get(text);
                    robot.addSpeech(response);
                }
            }
        }

    }

    public static void main(String args[]) {
        WozApp app = new WozApp();
        app.start();
    }

}
